using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.ItemMasters
{
    [Authorize]
    public class ItemMasterAppService : BasicArchivesAppService, IItemMasterAppService
    {
        protected IItemMasterRepository ItemMasterRepository => LazyServiceProvider.LazyGetRequiredService<IItemMasterRepository>();
        protected ItemMasterManager ItemMasterManager => LazyServiceProvider.LazyGetRequiredService<ItemMasterManager>();

        [Authorize(BasicArchivesPermissions.ItemMaster.Query)]
        public async Task<ItemMasterDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<ItemMaster, ItemMasterDto>(
                await ItemMasterRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Query)]
        public async Task<ItemMasterDto> FindAsync(string filter)
        {
            var entity = await ItemMasterRepository.FindAsync(x => x.Name == filter);
            return ObjectMapper.Map<ItemMaster, ItemMasterDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Query)]
        public async Task<ItemMasterDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await ItemMasterRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<ItemMaster, ItemMasterDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Query)]
        public async Task<PagedResultDto<ItemMasterDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await ItemMasterRepository.GetCountAsync(input.Filter, true);
            var list = await ItemMasterRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<ItemMasterDto>(
                totalCount,
                ObjectMapper.Map<List<ItemMaster>, List<ItemMasterDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Query)]
        public async Task<PagedResultDto<ItemMasterDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<ItemMaster, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<ItemMaster>(input.FilterGroup);
            }

            var totalCount = await ItemMasterRepository.GetCountAsync(expression, true);
            var list = await ItemMasterRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<ItemMasterDto>(
                totalCount,
                ObjectMapper.Map<List<ItemMaster>, List<ItemMasterDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Create)]
        public async Task<ItemMasterDto> CreateAsync(ItemMasterCreateDto input)
        {
            var entity = ObjectMapper.Map<ItemMasterCreateDto, ItemMaster>(input);

            await ItemMasterManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Update)]
        public async Task<ItemMasterDto> UpdateAsync(Guid id, ItemMasterUpdateDto input)
        {
            var entity = await ItemMasterRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await ItemMasterManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.ItemMaster.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await ItemMasterManager.DeleteAsync(id);
        }
    }
}